<template>
  <view class="driving-container">
    <u-navbar title="班型列表" :autoBack="false"></u-navbar>
    
    <scroll-view 
      scroll-y="true"
      class="scroll-container"
      refresher-enabled="true"
      :refresher-triggered="refreshing"
      @refresherrefresh="onRefresh"
      @scrolltolower="loadMore"
      @scroll="onScroll"
      :lower-threshold="50"
    >
      <view class="class-item" v-for="(classType, index) in classList" :key="index" @click="goToClassDetail(classType)">
        <view class="class-image">
          <image :src="classType.image" mode="aspectFill"></image>
        </view>
        <view class="class-info">
          <view class="class-name">{{ classType.name }}</view>
          <view class="class-desc">{{ classType.description }}</view>
          <view class="class-features">
            <text class="feature-tag" v-for="(feature, featureIndex) in classType.features" :key="featureIndex">
              {{ feature }}
            </text>
          </view>
          <view class="class-meta">
            <text class="price">¥{{ classType.price }}</text>
            <text class="duration">{{ classType.duration }}</text>
          </view>
        </view>
      </view>
      
      <!-- 加载更多提示 -->
      <view class="load-more" v-if="classList.length > 0">
        <view v-if="loading" class="loading-text">加载中...</view>
        <view v-else-if="!hasNext" class="no-more-text">没有更多数据了</view>
        <view v-else class="load-more-text" @click="loadMore">点击加载更多</view>
      </view>
    </scroll-view>
  </view>
</template>

<script>
import { getClassList } from '@/src/api/driver/index.js'

export default {
  data() {
    return {
      refreshing: false,
      classList: [],
      // 分页相关
      currentPage: 1,
      pageSize: 5,
      hasNext: false,
      hasPrev: false,
      total: 0,
      totalPages: 0,
      loading: false,

    }
  },
  
  onLoad() {
    this.loadClassList();
  },
  
  methods: {
    async loadClassList(isRefresh = false) {
      if (this.loading) return;
      
      this.loading = true;
      try {
        const response = await getClassList(this.currentPage, this.pageSize);
        console.log('API response:', response);
        if (response.code === 200) {
          if (isRefresh) {
            this.classList = response.data;
          } else {
            this.classList = [...this.classList, ...response.data];
          }
          
          // 更新分页信息
          this.hasNext = response.pagination.hasNext;
          this.hasPrev = response.pagination.hasPrev;
          this.total = response.pagination.total;
          this.totalPages = response.pagination.totalPages;
          console.log('Updated state:', { hasNext: this.hasNext, currentPage: this.currentPage, total: this.total, classListLength: this.classList.length });
        } else {
          uni.showToast({
            title: response.message || '获取数据失败',
            icon: 'none'
          });
        }
      } catch (error) {
        uni.showToast({
          title: '网络错误',
          icon: 'none'
        });
      } finally {
        this.loading = false;
      }
    },
    
    async loadMore() {
      console.log('loadMore called', { loading: this.loading, hasNext: this.hasNext, currentPage: this.currentPage });
      if (this.loading || !this.hasNext) {
        console.log('loadMore blocked', { loading: this.loading, hasNext: this.hasNext });
        return;
      }
      
      this.currentPage++;
      console.log('Loading page', this.currentPage);
      await this.loadClassList();
    },
    
    async onRefresh() {
      this.refreshing = true;
      this.currentPage = 1;
      
      try {
        await this.loadClassList(true);
        uni.showToast({
          title: '刷新成功',
          icon: 'success'
        });
      } catch (error) {
        uni.showToast({
          title: '刷新失败',
          icon: 'none'
        });
      } finally {
        this.refreshing = false;
      }
    },
    
    goToClassDetail(classType) {
      // 可以跳转到班型详情页面
      uni.showToast({
        title: `查看${classType.name}详情`,
        icon: 'none'
      });
    },
    
    onScroll(e) {
      console.log('Scroll event:', e.detail);
    }
  }
}
</script>

<style scoped lang="scss">
.driving-container {
  background: #f5f5f5;
  min-height: 100vh;
  padding: 30rpx 0;
  padding-top: 180rpx;
  margin-top: 20rpx;
  margin-bottom: 20rpx;
  box-sizing: border-box;
}

.scroll-container {
  height: calc(100vh - 200rpx);
}

.class-item {
  background: #fff;
  border-radius: 16rpx;
  box-shadow: 0 2rpx 10rpx rgba(0,0,0,0.06);
  margin: 0 30rpx 30rpx 30rpx;
  overflow: hidden;
  display: flex;
  align-items: center;
  padding: 20rpx;
}

.class-image {
  width: 120rpx;
  height: 120rpx;
  border-radius: 8rpx;
  overflow: hidden;
  margin-right: 20rpx;
  flex-shrink: 0;
  
  image {
    width: 100%;
    height: 100%;
  }
}

.class-info {
  flex: 1;
  display: flex;
  flex-direction: column;
}

.class-name {
  font-size: 28rpx;
  font-weight: bold;
  color: #333;
  margin-bottom: 8rpx;
}

.class-desc {
  font-size: 24rpx;
  color: #666;
  margin-bottom: 12rpx;
  line-height: 1.4;
}

.class-features {
  display: flex;
  flex-wrap: wrap;
  gap: 8rpx;
  margin-bottom: 12rpx;
}

.feature-tag {
  font-size: 20rpx;
  color: #1296db;
  background: #e6f3ff;
  border-radius: 6rpx;
  padding: 4rpx 12rpx;
}

.class-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.price {
  font-size: 26rpx;
  font-weight: bold;
  color: #ff6b35;
}

.duration {
  font-size: 22rpx;
  color: #999;
}

.load-more {
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 30rpx 0;
}

.loading-text,
.no-more-text,
.load-more-text {
  font-size: 24rpx;
  color: #999;
}


</style> 